package com.qf.controller;


import com.qf.pojo.Page;
import com.qf.pojo.Profile;
import com.qf.pojo.User;
import com.qf.service.RecordService;
import com.qf.service.RecordServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/recordList")
public class RecordServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");

        RecordService service = new RecordServiceImpl();
        //查询book表的所有数据。
//        List<Book> books = service.getAll();
        //获取页数
        String pageIndex = request.getParameter("pageIndex");
        //使用jsp来展示书籍列表
//        如果重定向到 jsp页面，需要将数据保存到 session
        //如果使用请求转发，可以将数据保存到 request
        // request.setAttribute("bookList", books);
        if (pageIndex==null){
            pageIndex="1";
        }
//        封装Page分页对象，第一个参数：当前需要跳转的页码，第二个参数：每页显示条数
        Page page = new Page(Integer.parseInt(pageIndex), 5);

        List<Profile>recordList = service.findAllPage(page);

        int count = service.findAllPageCount();

        page.setTotalCounts(count);

        request.setAttribute("recordList",recordList);

        request.setAttribute("page",page);

        request.setAttribute("pageIndex",pageIndex);

        request.getRequestDispatcher("/recordList.jsp").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
